package com.umeox.watch.moto.chat.db

import com.umeox.watch.moto.dataservice.DataProvider
import java.lang.Exception
import kotlin.collections.ArrayList

/**
 * Created by Rambo 2021/8/19
 * Describe:
 */
object DBUtils {

    /**
     * 获取好友列表
     * 1.通过Content URI 先获取服务器下发的好友，然后对比本地的好友，删除不存在的好友已经聊天记录
     * 2.返回按照最后一次聊天时间或者好友创建时间排序后的好友列表
     */
    fun getContactList(): List<ContactInfo> {
        return AppDatabase.get().contactInfoDao().loadAllContacts()
    }

    fun getContactInfoByFriend(friendId: String): ContactInfo? {
        return AppDatabase.get().contactInfoDao().queryByFriendId(friendId)
    }

    fun getBuddiesFromProvider(): List<ContactInfo> {
        var result: List<ContactInfo> = ArrayList()
        try {
            val adminId = DataProvider.getAdmin()
            val followers = DataProvider.getBuddies()
            if (followers != null && followers.isNotEmpty()) {
                result = ArrayList(followers.size)
                for (follower in followers) {
                    val bean = ContactInfo()
                    val friendId = follower[DataProvider.BuddyProvider.FRIEND_ID] as String
                    bean.friendId = friendId
                    bean.isAdmin = friendId.equals(adminId, ignoreCase = true)
                    bean.type = follower[DataProvider.BuddyProvider.TYPE] as Int
                    bean.name = follower[DataProvider.BuddyProvider.NICKNAME] as String
                    val avatar = follower[DataProvider.BuddyProvider.NICKNAME]
                    if (avatar != null) bean.avatar = avatar as String else bean.avatar = ""
                    bean.createTime =
                        (follower[DataProvider.BuddyProvider.CREATE_TIME] as Long).toInt()
                    result.add(bean)
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return result
    }

    fun getIMGroupFromProvider(): List<ContactInfo> {
        var result: List<ContactInfo> = ArrayList()
        try {
            val imGroups = DataProvider.getIMGroups()
            if (imGroups != null && imGroups.isNotEmpty()) {
                result = ArrayList(imGroups.size)
                for (imGroup in imGroups) {
                    val bean = ContactInfo()
                    bean.friendId = imGroup[DataProvider.IMGroupProvider.ID] as String
                    bean.type = imGroup[DataProvider.IMGroupProvider.TYPE] as Int
                    bean.name = imGroup[DataProvider.IMGroupProvider.NAME] as String
                    val avatar = imGroup[DataProvider.IMGroupProvider.AVATAR]
                    if (avatar != null) bean.avatar = avatar as String else bean.avatar = ""
                    bean.createTime =
                        (imGroup[DataProvider.IMGroupProvider.CREATE_TIME] as Long).toInt()
                    bean.isGroup = true
                    result.add(bean)
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return result
    }

    fun loadAllMessageByFriendId(friendId: String): MutableList<ChatMessage?>? {
        return AppDatabase.get().chatMessageDao().loadAllMessageByFriendId(friendId)
    }

    fun loadGroupMessageByGroupId(friendId: String): MutableList<ChatMessage?>? {
        return AppDatabase.get().chatMessageDao().loadGroupMessageByFriendId(friendId)
    }
}